/**
 * @file conder.c
 * @author      JinChengLong (https://gitee.com/Datejin/)
 * @brief       编码器和索引相关
 * @version     0.1
 * @date        2023
 * 
 * @copyright   Copyright © 2025 by JinChengLong 
 * 
 * All rights reserved.
 * Unauthorized copying, reproduction, or redistribution of this file via any medium is strictly prohibited.  
 * Proprietary and confidential.
 *   
 */
#include "conder.h"




int32_t q=0, conde=4;

/**
 * @brief 编码器初始化
 * 
 */
void encoder_init(void)
{
	
	
	
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);			//初始化AFIO
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);			//初始化PA0引脚
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);			//打开TIM2定时器时钟
	
	
	////引脚重映射配置//PA0转PA15
	GPIO_PinRemapConfig(GPIO_FullRemap_TIM3, ENABLE);//选择完全重映射1（由手册可知只改俩引脚
	
	
	
	//初始化TIM2通道
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_5 ;
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;			//上拉输入//外部模块默认输入状态//一般默认高电平//若未知则浮空（则会受到干扰）
	GPIO_Init(GPIOC,&GPIO_InitStructure);
	
	
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5 ;
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPD;			//上拉输入//外部模块默认输入状态//一般默认高电平//若未知则浮空（则会受到干扰）
	GPIO_Init(GPIOC,&GPIO_InitStructure);
	//不需要时钟
	
	//时基单元配置
	//（误差1/计数值）//其他信号晶振含误差
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
	TIM_TimeBaseInitStructure.TIM_ClockDivision =TIM_CKD_DIV1;		//TIM2 1分频
	TIM_TimeBaseInitStructure.TIM_CounterMode =TIM_CounterMode_Up;	//向上计数(从零到重装值）
	TIM_TimeBaseInitStructure.TIM_RepetitionCounter =0;				//重复计数器的值（高级定时器）
	TIM_TimeBaseInitStructure.TIM_Prescaler	=1-1;//PSC不分频//
	TIM_TimeBaseInitStructure.TIM_Period =65536-1;//ARR自动重装器的值（周期）（0到65535）设置为最大防止计数溢出
	//1MHZ//最低频率为1M/65535=15HZ（应加大PSC）//上限1MHZ																		//计算一秒的时间10k的频率计算10000个数，一秒一个周期
	TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStructure);
	
	//初始化输入捕获单元（仅用边沿选择和滤波器）
	TIM_ICInitTypeDef TIM_ICInitStructure;
	TIM_ICStructInit(&TIM_ICInitStructure);
	TIM_ICInitStructure.TIM_Channel=TIM_Channel_1;//配置3.1通道
	//TIM_ICInitStructure.TIM_ICPolarity=TIM_ICPolarity_Rising;//极性选择//上升沿触发//仅代表极性不反相
	TIM_ICInitStructure.TIM_ICFilter=0xF;//输入捕获的滤波器选择//防止干扰//数越大滤波效果越好//滤波器采样频率较高信号频率不变
	
	TIM_PWMIConfig(TIM3,&TIM_ICInitStructure);//顺便配置通道2交叉下降沿（仅通道1，2）
	
	TIM_ICStructInit(&TIM_ICInitStructure);
	TIM_ICInitStructure.TIM_Channel=TIM_Channel_2;
	//TIM_ICInitStructure.TIM_ICPolarity=TIM_ICPolarity_Rising;
	TIM_ICInitStructure.TIM_ICFilter=0xF;
	
	TIM_PWMIConfig(TIM3,&TIM_ICInitStructure);
	
	
	//后面Encoder覆盖前面
	TIM_EncoderInterfaceConfig(TIM3,TIM_EncoderMode_TI12,TIM_ICPolarity_Rising,TIM_ICPolarity_Rising);//TI1,2都计数，都不反相
	
	TIM_Cmd(TIM3,ENABLE);
	
	
}

/**
 * @brief 获取编码器
 * 
 * @return int16_t 
 */
int16_t encoder_get(void)
{
	int16_t temp=0;
	temp=TIM_GetCounter(TIM3);//CNT付给temp
	//TIM_SetCounter(TIM3,0);
	return temp;
}

/**
 * @brief 编码器清零
 * 
 */
void encoder_over(void )
{
	TIM3->CNT=0;
	
}


//主界面滚动选择
/**
 * @brief 主界面索引
 * 
 */
void conde_one(void)
{
	
	if(encoder_get()>=4 & encoder_get()%4==0)
	{
		switch (encoder_get()%3)
		{
			case 1:conde=12;break;
			case 2:conde=8;break;
			case 0:conde=4;break;				
		}
		
	}
	
	
	else if(encoder_get()<=-4 & (-encoder_get()) % 4==0)
	{			
		switch ((-encoder_get())%3)
		{
			case 1:conde=8;break;
			case 2:conde=12;break;
			case 0:conde=4;break;				
		}
		
	}
	else if(encoder_get()==0)
		{
			conde=4;
		}
	
}

//复界面滚动选择
/**
 * @brief 复界面滚动索引
 * 
 */
void conde_two(void)
{
	
	if(encoder_get()>=4 & encoder_get()%4==0)
		{
			switch (encoder_get()%48/4)
			{
				case 1:conde=4;break;
				case 2:conde=8;break;
				case 3:conde=12;break;	
				case 4:conde=16;break;
				case 5:conde=20;break;
				case 6:conde=24;break;
				case 7:conde=28;break;
				case 8:conde=32;break;
				case 9:conde=36;break;	
				case 10:conde=40;break;
				case 11:conde=44;break;
				case 0:conde=48;break;
			
			}
		}
		
		
		
		else if(encoder_get()<=-4 & (-encoder_get()) % 4==0)
		{			
			switch ((-encoder_get())%48/4)
			{
				case 1:conde=4;break;
				case 2:conde=8;break;
				case 3:conde=12;break;	
				case 4:conde=16;break;
				case 5:conde=20;break;
				case 6:conde=24;break;
				case 7:conde=28;break;
				case 8:conde=32;break;
				case 9:conde=36;break;	
				case 10:conde=40;break;
				case 11:conde=44;break;
				case 0:conde=48;break;
			
			}
		}
	
}

/**
 * @brief 第三级界面滚动索引
 * 
 */
void conde_three(void)
{
			
	if(encoder_get()>=4 & encoder_get()%4==0)
		{
			switch (encoder_get()%20/4)
			{
				case 1:conde=4;break;
				case 2:conde=8;break;
				case 3:conde=12;break;	
				case 4:conde=16;break;			
				case 0:conde=20;break;
			}
		}
		else if(encoder_get()<=-4 & (-encoder_get()) % 4==0)
		{			
			switch ((-encoder_get())%20/4)
			{
				case 1:conde=4;break;
				case 2:conde=8;break;
				case 3:conde=12;break;	
				case 4:conde=16;break;
				case 0:conde=20;break;
			}
		}
		else if(encoder_get()==0)
		{
			conde=4;
		}
	
}


uint16_t  S(uint16_t a,uint16_t b)

{
	return (a-a%b)/b;
	
}















